<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Data_pencarian extends CI_Controller
{
	function __construct(){
		parent::__construct();	
		$this->load->library('form_validation');			
		$this->load->model('ipkd_model');		
	}
	
	function index()
	{		
		$this->data();
	}
	
	function menu_pencarian()
	{
		$kecamatan = $this->ipkd_model->get_all_kecamatan();
		
		$kecamatan_options['all'] = '-- Pilih Semua Kecamatan --';
					foreach($kecamatan->result() as $kcm){
					$kecamatan_options[$kcm->NAMA_KECAMATAN] = $kcm->NAMA_KECAMATAN;
					}
		$program_options['all']= '-- Pilih Semua Program  --';
		$program_options['ipkd']= 'Data Ijin Pemakaian Kekayaan Daerah';
		$program_options['dpb']= 'Data Peil Banjir';
		$program_options['dibsa']= 'Data Batas Sempadan Air';
		
		$data['program_options'] = $program_options;
		$data['kecamatan_options'] = $kecamatan_options;
		
		$data['content'] = $this->load->view('menu_pencarian',$data,true);
		$this->load->view('main',$data);
	
	}
	
	function data()
	{	
		$program = $this->input->post('program');
		$tahun = $this->input->post('tahun');
		$kecamatan = $this->input->post('kecamatan');
		
		if ($this->cek_validasi_1() == FALSE){
			$kecamatan = $this->ipkd_model->get_all_kecamatan();
		
			$kecamatan_options['all'] = '-- Pilih Semua Kecamatan --';
						foreach($kecamatan->result() as $kcm){
						$kecamatan_options[$kcm->NAMA_KECAMATAN] = $kcm->NAMA_KECAMATAN;
						}
			$program_options['all']= '-- Pilih Semua Program  --';
			$program_options['ipkd']= 'Data Ijin Pemakaian Kekayaan Daerah';
			$program_options['dpb']= 'Data Peil Banjir';
			$program_options['dibsa']= 'Data Batas Sempadan Air';
			
			$data['program_options'] = $program_options;
			$data['kecamatan_options'] = $kecamatan_options;
			
			$data['content'] = $this->load->view('menu_pencarian',$data,true);
			$this->load->view('main',$data);
		}
		else
		{

		if($program == "all")
		{
			if($kecamatan == "all")
			{
			$program_ipkd = $this->ipkd_model->get_all_ipkd_thn($tahun);
			$program_dibsa = $this->ipkd_model->get_all_dibsa_thn($tahun);
			$program_dpb = $this->ipkd_model->get_all_dpb_thn($tahun);
			
			$data['program_ipkd'] = $program_ipkd;
			$data['program_dpb'] = $program_dpb;
			$data['program_dibsa'] = $program_dibsa;
			
			$data['content'] = $this->load->view('pencarian_result',$data,true);
			$this->load->view('main',$data);
			}
			else
			{
			$program_ipkd = $this->ipkd_model->get_all_ipkd_cari($tahun,$kecamatan);
			$program_dibsa = $this->ipkd_model->get_all_dibsa_cari($tahun,$kecamatan);
			$program_dpb = $this->ipkd_model->get_all_dpb_cari($tahun,$kecamatan);
			
			$data['program_ipkd'] = $program_ipkd;
			$data['program_dpb'] = $program_dpb;
			$data['program_dibsa'] = $program_dibsa;
			
			$data['content'] = $this->load->view('pencarian_result',$data,true);
			$this->load->view('main',$data);
			}
		}
		else if($program == "ipkd")
		{
			if($kecamatan == "all"){
				$program_ipkd = $this->ipkd_model->get_all_ipkd_thn($tahun);
				$data['program'] = $program_ipkd;
				$data['judul'] = "Ijin Pemakaian Kekayaan Daerah";
				
				$data['content'] = $this->load->view('pencarian_result_tunggal',$data,true);
				$this->load->view('main',$data);
			}
			else{
				$program_ipkd = $this->ipkd_model->get_all_ipkd_cari($tahun,$kecamatan);
				$data['program'] = $program_ipkd;
				$data['judul'] = "Ijin Pemakaian Kekayaan Daerah";
				
				$data['content'] = $this->load->view('pencarian_result_tunggal',$data,true);
				$this->load->view('main',$data);
			}
		}
		else if($program == "dpb")
		{
			if($kecamatan == "all"){
				$program_dpb = $this->ipkd_model->get_all_dpb_thn($tahun);
				$data['program'] = $program_dpb;
				$data['judul'] = "Peil Banjir";
				
				$data['content'] = $this->load->view('pencarian_result_tunggal',$data,true);
				$this->load->view('main',$data);
			}
			else{
				$program_dpb = $this->ipkd_model->get_all_dpb_cari($tahun,$kecamatan);
				$data['program'] = $program_dpb;
				$data['judul'] = "Peil Banjir";
				
				$data['content'] = $this->load->view('pencarian_result_tunggal',$data,true);
				$this->load->view('main',$data);
			}
		}
		else if($program == "dibsa")
		{
			if($kecamatan == "all"){
				$program_dibsa = $this->ipkd_model->get_all_dibsa_thn($tahun);
				$data['program'] = $program_dibsa;
				$data['judul'] = "Batas Sempadan Air";
				
				$data['content'] = $this->load->view('pencarian_result_tunggal',$data,true);
				$this->load->view('main',$data);
			}
			else{
				$program_dibsa = $this->ipkd_model->get_all_dibsa_cari($tahun,$kecamatan);
				$data['program'] = $program_dibsa;
				$data['judul'] = "Batas Sempadan Air";
				
				$data['content'] = $this->load->view('pencarian_result_tunggal',$data,true);
				$this->load->view('main',$data);
			}
		}
		
		
		}
	}

	function cek_validasi() {	
			// Setting Rules
			
			$this->form_validation->set_rules('tgl1','Mulai Tanggal','required');
			$this->form_validation->set_rules('tgl2','Sampai Tanggal','required');
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!');

			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
			
	}

	function cek_validasi_1() {	
			// Setting Rules
			$this->form_validation->set_rules('tahun', 'Tahun', 'callback_cek_dropdown');
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!');

			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
			
	}

	function cek_dropdown($value){
	$tahun= $this->input->post('tahun');
		if($tahun == "all"){
			$this->form_validation->set_message('cek_dropdown', 'Kolom %s wajib dipilih!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}

	//buat yang bulan..........................................................................

	function menu_pencarian_bulan()
	{
		$kecamatan = $this->ipkd_model->get_all_kecamatan();
		
		$kecamatan_options['all'] = '-- Pilih Semua Kecamatan --';
					foreach($kecamatan->result() as $kcm){
					$kecamatan_options[$kcm->NAMA_KECAMATAN] = $kcm->NAMA_KECAMATAN;
					}
		
		$data['kecamatan_options'] = $kecamatan_options;
		
		$data['content'] = $this->load->view('menu_pencarian_bulan',$data,true);
		$this->load->view('main',$data);
	
	}

	function data_bulan()
	{	
		$kecamatan = $this->input->post('kecamatan');

		$f_tgl1 = str_replace("/","-",$_REQUEST['tgl1']);
		$tgl1 = date("Y-m-d", strtotime($f_tgl1));

		$f_tgl2 = str_replace("/","-",$_REQUEST['tgl2']);
		$tgl2 = date("Y-m-d", strtotime($f_tgl2));

		
		if ($this->cek_validasi() == FALSE){
			$kecamatan = $this->ipkd_model->get_all_kecamatan();
		
			$kecamatan_options['all'] = '-- Pilih Semua Kecamatan --';
						foreach($kecamatan->result() as $kcm){
						$kecamatan_options[$kcm->NAMA_KECAMATAN] = $kcm->NAMA_KECAMATAN;
						}

			$data['kecamatan_options'] = $kecamatan_options;
			
			$data['content'] = $this->load->view('menu_pencarian_bulan',$data,true);
			$this->load->view('main',$data);
		}
		else
		{

			if($kecamatan == "all"){
				$program_ipkd = $this->ipkd_model->get_all_ipkd_bln($tgl1,$tgl2);
				$data['program'] = $program_ipkd;
				$data['judul'] = "Ijin Pemakaian Kekayaan Daerah";
				
				$data['content'] = $this->load->view('pencarian_result_tunggal',$data,true);
				$this->load->view('main',$data);
			}
			else{
				$program_ipkd = $this->ipkd_model->get_all_ipkd_cari_bln($tgl1,$tgl2,$kecamatan);
				$data['program'] = $program_ipkd;
				$data['judul'] = "Ijin Pemakaian Kekayaan Daerah";
				
				$data['content'] = $this->load->view('pencarian_result_tunggal',$data,true);
				$this->load->view('main',$data);
			}
		
		
		
		}
	}

}//end of class